package com.zhihu_lvliangi_android.option.zhengwu.adapter;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.utils.SelecterUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.NewItem;
import com.zhihu_lvliangi_android.option.HomePageActivity;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.util.RequestURL;
import com.zhihu_lvliangi_android.util.ViewHolder;

/**
 * 新闻列表适配器
 * 
 * @author azuryleaves
 * @since 2014-3-13 下午5:23:24
 * @version 1.0
 * 
 */
public class NewsListViewAdapter extends BaseAdapter {

	private List<NewItem> data;
	private LayoutInflater inflater;
	private BitmapUtils bitmapUtil;
	private Context mContext;
	private BitmapDisplayConfig mDisplayConfig;

	public NewsListViewAdapter(Context context, List<NewItem> data) {
		super();
		mContext = context;

		inflater = LayoutInflater.from(context);
		bitmapUtil = new BitmapUtils(context);
		this.data = data;
		mDisplayConfig = MobileApplication.getInstance().initBitMapConfig(R.drawable.rotate_loading_360, R.drawable.rotate_loading_360);
	}
	
	
	public List<NewItem> getData() {
		return data;
	}


	public void setData(List<NewItem> data) {
		this.data = data;
	}


	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_news_item,null );
		}
		NewItem item = data.get(position);
		TextView tagView = ViewHolder.get(convertView, R.id.news_tag);
		tagView.setText(item.getNews_title());
		TextView pubtimeView = ViewHolder.get(convertView, R.id.news_pubtimes);
		pubtimeView.setText(TextUtils.isEmpty(item.getRelease_time())?"暂无此信息":item.getRelease_time().split(" ")[0]);
		
		if (position %2 == 1) {
			convertView.setBackground(SelecterUtil.setSelector(mContext, R.color.white, R.color.main_items_press, R.color.main_items_press, R.color.white));;
		}else {
			convertView.setBackground(SelecterUtil.setSelector(mContext, R.color.departitem_item_blue, R.color.main_items_press, R.color.main_items_press, R.color.white));;
		}
		return convertView;
	}


	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
